package com.cta.utils;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;

public class CDRStringUtils {

	private static ObjectMapper mapper = null;

	public static ObjectMapper getObjectMapper() {
		if (mapper == null) {
			mapper = new ObjectMapper();
		}
		return mapper;
	}

	public static double ob2double(Object ob) {
		if (ob == null) {
			return 0;
		}
		try {
			Double v = (Double) ob;
			return v;
		} catch (Exception e) {
			return Double.valueOf((Integer) ob);
		}
	}

	public static String toString(Object o) {
		try {
			return getObjectMapper().writeValueAsString(o);
		} catch (Exception e) {
			return null;
		}
	}
	
	public  static <T> T parseJsonType(String json, Class<T> valueType) {
		try {
			 getObjectMapper().configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
			return getObjectMapper().readValue(json, valueType);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
